package chapter18;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/3/27 20:29
 */
public class GZipCompress {
    public static void compress(String fileName) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(fileName));
        String zipFileName = fileName + ".gz";
        OutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(zipFileName)));
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

    public static void readCompress(String fileName) throws IOException {
        InputStream in = new BufferedInputStream(new GZIPInputStream(new FileInputStream(fileName)));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}
